﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XnaXaml.Controls;
using XnaXaml.Graphics;

namespace XnaXaml.Xaml
{
    public class GenericXamlRenderer : XamlRenderer
    {
        #region Properties

        public string FontAssetName { get; set; }
        public SpriteFont Font { get; set; }
        
        #endregion

        #region Constructor
        
        public GenericXamlRenderer(Game game, string fontAssetName, string xamlAssetName) : base(game, xamlAssetName)
        {
            FontAssetName = fontAssetName;
            GraphicsHelper.Initialize((GraphicsDeviceManager)game.Services.GetService(typeof (GraphicsDeviceManager)));
        }

        #endregion

        #region Methods

        protected override void LoadContent()
        {
            base.LoadContent();
            Font = Game.Content.Load<SpriteFont>(FontAssetName);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsHelper.CacheSpriteBatch(SpriteBatch);
                base.Draw(gameTime);
            GraphicsHelper.ResetCachedSpriteBatch();
        }

        #endregion

        #region Control Rendering

        public override void RenderBorder(Border ctrl)
        {
            var x = (int) ctrl.Position.X;
            var y = (int) ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);
            var thickness = new Thickness(ctrl.BorderThickness);

            GraphicsHelper.DrawRectangle(x, y, (int)size.X, (int)size.Y, thickness, ctrl.BorderBrush);
        }

        public override void RenderGrid(Grid ctrl)
        {
            // Not Visible
        }

        public override void RenderPage(Page ctrl)
        {
            // Not Visible
        }

        public override void RenderButton(Button ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);
            var text = ctrl.Content != null ? ctrl.Content as string : "";

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);

            var textSize = Font.MeasureString(text);
            var textPosition = new Vector2(x + (size.X/2) - (textSize.X/2), y + (size.Y/2) - (textSize.Y/2));
            SpriteBatch.DrawString(Font, text, textPosition, Color.Black);
        }

        public override void RenderCheckBox(CheckBox ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);
            var text = ctrl.Content != null ? ctrl.Content as string : "";

            GraphicsHelper.DrawFilledRectangle(x, y, (int)16, (int)16, Thickness.Default, Color.Black, Color.Silver);

            var textSize = Font.MeasureString(text);
            var textPosition = new Vector2((x + 24), y + (size.Y / 2) - (textSize.Y / 2));
            SpriteBatch.DrawString(Font, text, textPosition, Color.Black);
        }

        public override void RenderComboBox(ComboBox ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);
            var text = "+";

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);
            GraphicsHelper.DrawFilledRectangle(x + (size.X - size.Y), y, (int)size.Y, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);

            var textSize = Font.MeasureString(text);
            var textPosition = new Vector2((x + (size.X - size.Y) + (size.Y / 3)), y + (size.Y / 2) - (textSize.Y / 2));
            SpriteBatch.DrawString(Font, text, textPosition, Color.Black);
        }

        public override void RenderImage(Image ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);
        }

        public override void RenderLabel(Label ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);
            var text = ctrl.Content != null ? ctrl.Content as string : "";

            var textSize = Font.MeasureString(text);
            var textPosition = new Vector2(x, y + (size.Y / 2) - (textSize.Y / 2));
            SpriteBatch.DrawString(Font, text, textPosition, Color.Black);
        }

        public override void RenderListBox(ListBox ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);
        }

        public override void RenderRadioButton(RadioButton ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);

            var text = ctrl.Content != null ? ctrl.Content as string : "";

            GraphicsHelper.DrawFilledCircle(x + 6, y + 6, 6f, Color.Black, Color.Silver);

            var textSize = Font.MeasureString(text);
            var textPosition = new Vector2((x + 16), y + (size.Y / 2) - (textSize.Y / 2));
            SpriteBatch.DrawString(Font, text, textPosition, Color.Black);
        }

        public override void RenderStackPanel(StackPanel ctrl)
        {
            // Not Visible
        }

        public override void RenderTabControl(TabControl ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);
        }

        public override void RenderTabItem(TabItem ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);
        }

        public override void RenderTextBlock(TextBlock ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);
        }

        public override void RenderTextBox(TextBox ctrl)
        {
            var x = (int)ctrl.Position.X;
            var y = (int)ctrl.Position.Y;
            var size = new Vector2(ctrl.Width, ctrl.Height);

            GraphicsHelper.DrawFilledRectangle(x, y, (int)size.X, (int)size.Y, Thickness.Default, Color.Black, Color.Silver);
        }

        #endregion
    }
}
